﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using SimpleJson;
using Pomelo.DotNetClient;

namespace SimpleClient
{
    public partial class InitForm : Form
    {
        private string _gate_server_ip = "127.0.0.1";
        private int _gate_server_port = 3014;
        public static JsonObject _users = null;
        public static PomeloClient _pomelo = null;
        public InitForm()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            _pomelo = new PomeloClient(_gate_server_ip, _gate_server_port);
            Console.WriteLine("开始连接 gate server  " + _gate_server_ip + ":" + _gate_server_port);
            _pomelo.connect(null, (data) =>
            {
                MessageBox.Show("成功连接 server ");
                JsonObject msg = new JsonObject();
                msg["uid"] = AccountNameEdit.Text;
                msg["pwd"] = PasswdEdit.Text;
                _pomelo.request("gate.gateHandler.login", msg, LoginServerCallback);
            });
        }
        void LoginServerCallback(JsonObject result)
        {
            if (Convert.ToInt32(result["code"]) == 200)
            {
                MessageBox.Show("登陆成功");
                MessageBox.Show(result["msg"].ToString());
                _pomelo.disconnect();
                LogintoServer(result);
                Console.WriteLine(result);
            }
            else
            {
                MessageBox.Show("登陆失败");
            }
        }
        void LogintoServer(JsonObject result)
        {

            string host = (string)result["host"];
            int port = Convert.ToInt32(result["port"]);

            _pomelo = new PomeloClient(host, port);

            _pomelo.connect(null, (data) =>
            {
                MessageBox.Show("成功连接 server ");
                JsonObject msg = new JsonObject();
                msg["uid"] = AccountNameEdit.Text;
                msg["pwd"] = PasswdEdit.Text;
                _pomelo.request("connector.entryHandler.login", msg, ChangeServerCallback);
            });
        }

        private void regLabel_Click(object sender, EventArgs e)
        {
            _pomelo = new PomeloClient(_gate_server_ip, _gate_server_port);
            Console.WriteLine("开始连接 gate server  " + _gate_server_ip + ":" + _gate_server_port);
            _pomelo.connect(null, (data) =>
            {
                MessageBox.Show("成功连接 server ");
                JsonObject msg = new JsonObject();
                msg["uid"] = AccountNameEdit.Text;
                msg["pwd"] = PasswdEdit.Text;
                _pomelo.request("gate.gateHandler.register", msg, RegisterServerCallback);
            });
        }
        void RegisterServerCallback(JsonObject result)
        {
            if (Convert.ToInt32(result["code"]) == 200)
            {
                _pomelo.disconnect();
                MessageBox.Show("注册成功");
                MessageBox.Show(result["msg"].ToString());
            }
            else
            {
                MessageBox.Show("注册失败");
            }
        }
        void ChangeServerCallback(JsonObject result)
        {
            if (Convert.ToInt32(result["code"]) == 200)
            {
                MessageBox.Show("跳转成功");
                MessageBox.Show(result["msg"].ToString());
                _pomelo.disconnect();
            }
            else
            {
                MessageBox.Show("登陆失败");
            }
        }

    }
}
